<template>
  <div>
    <!-- 登录头部 -->
    <van-nav-bar
      title="账号登录"
      left-arrow
    >
      <template #left>
        <van-icon  name="arrow-left" @click="$router.back()"/>
      </template>
    </van-nav-bar>

    <!-- form表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="form.password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="padding:0 15px">
        <van-button  color="#21b97a"  block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <div class="foot">
      <a href="#">还没有账号，去注册~</a>
    </div>
  </div>
</template>

<script>
import { getLogin } from '@/api/login'
import { Toast } from 'vant'
// import { setStorage } from '@/utils/storage'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },

  created () {

  },

  methods: {
    async onSubmit (values) {
      try {
        const res = await getLogin(values)
        if (res.data.body) {
          console.log(res)
          this.$store.commit('inituser', res.data.body.token)
          Toast('登录成功')
          this.$router.push('/home/profile')
        } else {
          Toast('账号密码不对')
        }
      } catch (error) {
        console.log('登录出错')
      }
    }
  }
}
</script>

<style scoped lang='less'>

.van-button {
  height: 50px;
  font-size: 18px;
}
/deep/.van-field__control{
  font-size: 17px;
  height: 60px;
}
.foot{
  margin-top: 30px;
  text-align: center;
  a {
    color: #666;
    font-size: 14px;
  }
}
</style>
